package com.fxp.project.jiuselu.web.models.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxp.project.jiuselu.commons.constant.ConstantCode;
import com.fxp.project.jiuselu.commons.exceptions.ParamException;
import com.fxp.project.jiuselu.commons.utils.RandomUtils;
import com.fxp.project.jiuselu.web.business.v1.pojo.UserInfoVo;
import com.fxp.project.jiuselu.web.business.v1.request.order.ReqOrderSample;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrderSample;
import com.fxp.project.jiuselu.web.models.order.mapper.JslOrderSampleMapper;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderSampleService;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 样品订单表 服务实现类
 * </p>
 *
 * @author Jason.Wang
 * @since 2021-11-23
 */
@Service
public class JslOrderSampleServiceImpl extends ServiceImpl<JslOrderSampleMapper, JslOrderSample> implements IJslOrderSampleService {

    @Autowired
    JslOrderSampleMapper jslOrderSampleMapper;
    @Autowired
    JslUserMapper jslUserMapper;

    /**
     * saveOrderSampleByUserId: 保存来样订单<br/>
     *
     * @param reqOrderSample
     * @param userId
     * @return
     * @author Kai.Wang
     * @date 2021/11/23 18:13
     */
    @Override
    @Transactional
    public Object saveOrderSampleByUserId(ReqOrderSample reqOrderSample, Long userId) {
        UserInfoVo userInfoVo = jslUserMapper.getUserInfoVoByUserId(userId);
        if (null == userInfoVo) {
            throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "用户信息不存在，请重试");
        }
        JslOrderSample jslOrderSample = new JslOrderSample();
        jslOrderSample.setUserId(userInfoVo.getUserId());
        jslOrderSample.setSampleSn(String.format("LY%s", RandomUtils.getOrderNo()));
        jslOrderSample.setNickName(userInfoVo.getCompanyName());
        jslOrderSample.setPhone(userInfoVo.getUserPhone());
        jslOrderSample.setName(reqOrderSample.getName());
        jslOrderSample.setSupplier(reqOrderSample.getSupplier());
        jslOrderSample.setLogistic(reqOrderSample.getLogistic());
        jslOrderSample.setLogisticOrder(reqOrderSample.getLogisticOrder());
        jslOrderSample.setCreateTime(LocalDateTime.now());
        jslOrderSample.setStatus(0);
        return jslOrderSampleMapper.insert(jslOrderSample);
    }

    @Override
    public JslOrderSample selectOrderSampleBylogisticOrder(String sampleSn) {
        return jslOrderSampleMapper.selectSampleBylogisticOrder(sampleSn);
    }
}
